<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;

class User extends AdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $guanli = Session::get('adname');
        $data = db('ad_user')->select();
        return view('main/userlist',[
            'data'=>$data,
            'guanli'=>$guanli
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $guanli = Session::get('adname');
        $time = date('Y-m-d H:i:s');
        $strtime = time();
//        dump($time);
        return view('main/useradd',[
            'time'=>$time,
            'strtime'=>$strtime,
            'guanli' => $guanli
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $p = $request->post();
//        dump($p);die;
        $data = [
            'name'=>$p['name'],
            'pwd'=>md5($p['pwd']),
            'createtime'=>$p['createtime']
        ];
//              dump($data);die;
//        $mname=strtolower($request->controller());
//        dump($mname);die;

        $res = db('ad_user')->data($data)->insert();

        if ($res > 0) {
            return $this->success('添加成功', url('admin/user/index'));
        } else {
            return $this->error('添加失败');
        }

    }


    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
//        var_dump($id);die;
        $guanli = Session::get('adname');
        $row = db('ad_user')->field('pass', true)->find($id);
        $time = date('Y-m-d H:i:s',$row['createtime']);
        $name = $row['name'];
        $id = $row['id'];
        return view('main/useredit', [
            'name' => $name,
            'time' => $time,
            'id' => $id,
            'guanli' => $guanli
        ]);

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $p = $request->put();

        $data = [
            'name' => $p['name'],
            'pwd' => md5($p['pwd'])
        ];

        $result = db('ad_user')->where('id',$id)->update($data);

        if ($result) {
            return $this->success('编辑成功', url('admin/user/index'));
        } else {
            return $this->error('编辑失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = db('ad_user')->delete($id);
        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的用户删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的用户删除失败!';
        }
        // JSON 返回
        return json($info);
    }

    //分配用户权限
    public function editrole($id)
    {
        $guanli = Session::get('adname');

        //查找该角色权限id
        $role_id = db('ad_user_role')->where('user_id',$id)->field('role_id')->select();
        //查找所有的角色
        $data = db('ad_role')->select();

        $roid = array();
       //遍历重组数组
        foreach($role_id as $v){
            $roid[] = $v['role_id'];
        }
//        dump($ro_nodes);die;
        return view('main/userrole',[
            'roid'=>$roid,
            'guanli' => $guanli,
            'data' => $data,
            'role_id' => $role_id,
            'uid' => $id
        ]);

    }

    //保存分配信息
    public function rolesave(Request $request)
    {
        $arr = array();
        $p = $request->post();
//        dump($p);die;
        $user_id = $p['uid'];

        if (!array_key_exists('a',$p)){
            $req = db('ad_user_role')->where('user_id',$user_id)->delete();
            if ($req > 0){
                return $this->success('编辑成功', url('admin/user/index'));
            }else{
                return $this->error('编辑失败');
            }
        }else{
            $data = $p['a'];
//        dump($data);die;

            $qu = db('ad_user_role')->field('user_id')->select();
//        dump($qu);die;
            foreach ($qu as $k=>$v){
                if (in_array($user_id,$v)){
                    $res = db('ad_user_role')->where('user_id',$user_id)->delete();
                }
            }

            foreach($data as $v){
                $arr['user_id'] = $user_id;
                $arr['role_id'] = $v;
                $rev = db('ad_user_role')->data($arr)->insert();
                $arr = [];
            }
            if ($rev > 0){
                return $this->success('编辑成功', url('admin/user/index'));
            }else{
                return $this->error('编辑失败');
            }
        }


    }

}
